package com.xhd.base.log.impl;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.OutputStreamWriter;
import java.util.Calendar;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Vector;

import com.xhd.base.log.Log;
import com.xhd.base.log.LogFactory;
import com.xhd.db.DBCenter;
import com.xhd.db.DBSQL;
import com.xhd.db.XDao;
import com.xhd.exception.DatabaseException;
import com.xhd.server.ServerConfig;

public class XLogWriter {
	 private int jm92 = 0;
	  private boolean jm93;
	  private String jm94;
	  private Hashtable jm95 = new Hashtable();
	  private Hashtable jm96 = new Hashtable();
	  public static XLogWriter instance = null;
	  private static final Log jm9;
	  static Class jm10;

	  static
	  {
	    Class tmp7_4 = jm10;
	    if (tmp7_4 == null)
	    {
	    
	      try
	      {
	       XLogWriter.jm10 = Class.forName("com.sinocc.base.log.impl.XLogWriter");
	      }
	      catch (ClassNotFoundException localClassNotFoundException)
	      {
	        throw new NoClassDefFoundError(localClassNotFoundException.getMessage());
	      }
	    }
	    jm9 = LogFactory.getLog(tmp7_4);
	  }

	  public void freeFileWriter()
	    throws IOException
	  {
	    Enumeration localEnumeration = this.jm95.keys();
	    while (localEnumeration.hasMoreElements())
	    {
	      String str = (String)localEnumeration.nextElement();
	      PrintWriter localPrintWriter = (PrintWriter)this.jm95.remove(str);
	      if (localPrintWriter == null)
	        continue;
	      localPrintWriter.close();
	    }
	  }

	  private DBCenter jm87(String paramString, boolean paramBoolean)
	  {
	    if (paramString == null)
	      paramString = ServerConfig.DefaultVault;
	    synchronized (this.jm96)
	    {
	      Object localObject1 = (DBCenter)this.jm96.get(paramString);
	      if (localObject1 == null)
	      {
	        localObject1 = new XDao(paramString);
	        ((DBCenter)localObject1).setTransation(false);
	        this.jm96.put(paramString, localObject1);
	      }
	      else if (paramBoolean)
	      {
	        try
	        {
	          ((DBCenter)localObject1).intSelect("select count(*) from cpcvault");
	        }
	        catch (Exception localException1)
	        {
	          try
	          {
	            jm9.info("Process Log database connection close, reconnect!");
	            ((DBCenter)localObject1).freeConnection();
	            localObject1 = new XDao(paramString);
	            ((DBCenter)localObject1).setTransation(false);
	            this.jm96.put(paramString, localObject1);
	          }
	          catch (Exception localException2)
	          {
	            localException1.printStackTrace();
	          }
	        }
	      }
	      return (DBCenter) localObject1;
	    }
	  }

	  private String jm88()
	    throws IOException
	  {
	    String str = this.jm94;
	    int i = str.lastIndexOf(ServerConfig.FILE_SEPARATOR);
	    if (i > 0)
	      i = str.indexOf(i, 46);
	    else
	      i = str.indexOf(46);
	    this.jm93 = (i > 0);
	    if (!(this.jm93))
	    {
	      Calendar localCalendar = Calendar.getInstance();
	      this.jm92 = (localCalendar.get(7) - 1);
	      this.jm92 = ((this.jm92 == 0) ? 7 : this.jm92);
	      str = str + String.valueOf(this.jm92) + ".log";
	    }
	    return str;
	  }

	  private PrintWriter jm89()
	    throws IOException
	  {
	    if (jm90())
	      freeFileWriter();
	    PrintWriter localPrintWriter = (PrintWriter)this.jm95.get(this.jm94);
	    if (localPrintWriter == null)
	    {
	      String str = jm88();
	      File localFile = new File(str);
	      if (localFile.exists())
	      {
	        long l = localFile.lastModified();
	        if (System.currentTimeMillis() - l > 86400000L)
	          localFile.delete();
	      }
	      try
	      {
	        localPrintWriter = new PrintWriter(new OutputStreamWriter(new FileOutputStream(str, true), "UTF-8"));
	        this.jm95.put(this.jm94, localPrintWriter);
	        System.out.println("Open log file " + jm88());
	      }
	      catch (IOException localIOException)
	      {
	        System.out.println("Can't open log file " + jm88());
	        localIOException.printStackTrace();
	      }
	    }
	    return localPrintWriter;
	  }

	  public static XLogWriter getInstance()
	  {
	    if (instance == null)
	      instance = new XLogWriter();
	    return instance;
	  }

	  private boolean jm90()
	  {
	    if (this.jm92 == 0)
	      return false;
	    if (!(this.jm93))
	    {
	      Calendar localCalendar = Calendar.getInstance();
	      int i = localCalendar.get(7) - 1;
	      i = (i == 0) ? 7 : i;
	      return (i != this.jm92);
	    }
	    return false;
	  }

	  public void setTheFileName(String paramString)
	  {
	    this.jm94 = paramString;
	  }

	  private void jm91(String paramString1, String paramString2)
	  {
	    String str = ServerConfig.SysLogVaultId;
	    if ((str == null) || (str.length() < 1))
	      str = paramString1;
	    DBCenter localDataCentre = jm87(str, false);
	    for (int i = 0; i < 2; ++i)
	    {
	      if (localDataCentre == null)
	      {
	        Exception localException = new Exception("Get log dao is null!");
	        jm9.error(localException);
	        jm9.error(paramString2);
	        return;
	      }
	      if ((localDataCentre.getDBType() == 1) && (paramString2.indexOf("syslogid") < 0))
	      {
	        int j = paramString2.indexOf(" cpcsyslog(");
	        int k = paramString2.indexOf(" values(");
	        if ((j > 0) && (k > 0))
	        {
	          j += 11;
	          k += 8;
	          StringBuffer localStringBuffer = new StringBuffer(paramString2.length() + 32);
	          localStringBuffer.append(paramString2.substring(0, j));
	          localStringBuffer.append("syslogid,");
	          localStringBuffer.append(paramString2.substring(j, k));
	          localStringBuffer.append("CPCSYSLOGSEQ.nextval,");
	          localStringBuffer.append(paramString2.substring(k));
	          paramString2 = localStringBuffer.toString();
	        }
	      }
	      try
	      {
	        localDataCentre.execute(paramString2);
	      }
	      catch (DatabaseException localDatabaseException)
	      {
	        localDataCentre = jm87(str, true);
	        return ;
	      }
	      catch (Throwable localThrowable)
	      {
	        jm9.error(null, localThrowable);
	        jm9.error(paramString2);
	      }
	      label255: return;
	    }
	  }

	  private void jm91(String paramString, DBSQL paramDBSQL)
	  {
	    String str = ServerConfig.SysLogVaultId;
	    if ((str == null) || (str.length() < 1))
	      str = paramString;
	    DBCenter localDataCentre = jm87(str, false);
	    for (int i = 0; i < 2; ++i)
	    {
	      Object localObject;
	      if (localDataCentre == null)
	      {
	        localObject = new Exception("Get log dao is null!");
	        jm9.error(localObject);
	        jm9.error(paramDBSQL.getSQL());
	        return;
	      }
	      if (localDataCentre.getDBType() == 1)
	      {
	        localObject = paramDBSQL.getSQL();
	        if (((String)localObject).indexOf("syslogid") < 0)
	        {
	          int j = ((String)localObject).indexOf(" cpcsyslog(");
	          int k = ((String)localObject).indexOf(" VALUES(");
	          if ((j > 0) && (k > 0))
	          {
	            j += 11;
	            k += 8;
	            StringBuffer localStringBuffer = new StringBuffer(((String)localObject).length() + 32);
	            localStringBuffer.append(((String)localObject).substring(0, j));
	            localStringBuffer.append("syslogid,");
	            localStringBuffer.append(((String)localObject).substring(j, k));
	            localStringBuffer.append("CPCSYSLOGSEQ.nextval,");
	            localStringBuffer.append(((String)localObject).substring(k));
	            paramDBSQL.setSQL(localStringBuffer.toString());
	          }
	        }
	      }
	      try
	      {
	        localDataCentre.execute(paramDBSQL);
	      }
	      catch (DatabaseException localDatabaseException)
	      {
	        localDataCentre = jm87(str, true);
	     //  break label274: //
	        	return ;
	      }
	      catch (Throwable localThrowable)
	      {
	        jm9.error(null, localThrowable);
	        jm9.error(paramDBSQL);
	      }
	      label274: return;
	    }
	  }

	  public void writeLogsToFile(Vector paramVector)
	  {
	    if (paramVector == null)
	      return;
	    try
	    {
	      int i = paramVector.size();
	      PrintWriter localPrintWriter = jm89();
	      for (int j = 0; j < i; ++j)
	      {
	        XLogItem localCPCLogItem = (XLogItem)paramVector.get(j);
	        if (localCPCLogItem.Message != null)
	          localPrintWriter.write(localCPCLogItem.Message + "\r\n");
	        if (localCPCLogItem.Cause != null)
	          localCPCLogItem.Cause.printStackTrace(localPrintWriter);
	        if (j % 100 != 0)
	          continue;
	        localPrintWriter.flush();
	      }
	      localPrintWriter.flush();
	    }
	    catch (Exception localException)
	    {
	      localException.printStackTrace();
	    }
	    paramVector = null;
	  }

	  public void writeLogsToDB(Vector paramVector)
	  {
	    if (paramVector == null)
	      return;
	    int i = paramVector.size();
	    for (int j = 0; j < i; ++j)
	    {
	      XLogItem localCPCLogItem = (XLogItem)paramVector.get(j);
	      if (localCPCLogItem.SQL2 != null)
	        jm91(localCPCLogItem.VaultId, localCPCLogItem.SQL2);
	      else
	        jm91(localCPCLogItem.VaultId, localCPCLogItem.SQL);
	    }
	  }
}
